package com.baobao.web;

import com.baobao.dao.CourseDAO;

import com.baobao.domain.Course;

import com.baobao.service.CourseService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Course entities
 * 
 */

@Controller("CourseController")
public class CourseController {

	/**
	 * DAO injected by Spring that manages Course entities
	 * 
	 */
	@Autowired
	private CourseDAO courseDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Course entities
	 * 
	 */
	@Autowired
	private CourseService courseService;

	/**
	 * Select the Course entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteCourse")
	public ModelAndView confirmDeleteCourse(@RequestParam String banjiIdKey, @RequestParam Integer weekDayKey, @RequestParam Integer seqNoKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("course", courseDAO.findCourseByPrimaryKey(banjiIdKey, weekDayKey, seqNoKey));
		mav.setViewName("course/deleteCourse.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/courseController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Select an existing Course entity
	 * 
	 */
	@RequestMapping("/selectCourse")
	public ModelAndView selectCourse(@RequestParam String banjiIdKey, @RequestParam Integer weekDayKey, @RequestParam Integer seqNoKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("course", courseDAO.findCourseByPrimaryKey(banjiIdKey, weekDayKey, seqNoKey));
		mav.setViewName("course/viewCourse.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Course entities
	 * 
	 */
	public String indexCourse() {
		return "redirect:/indexCourse";
	}

	/**
	 * Delete an existing Course entity
	 * 
	 */
	@RequestMapping("/deleteCourse")
	public String deleteCourse(@RequestParam String banjiIdKey, @RequestParam Integer weekDayKey, @RequestParam Integer seqNoKey) {
		Course course = courseDAO.findCourseByPrimaryKey(banjiIdKey, weekDayKey, seqNoKey);
		courseService.deleteCourse(course);
		return "forward:/indexCourse";
	}

	/**
	 * Save an existing Course entity
	 * 
	 */
	@RequestMapping("/saveCourse")
	public String saveCourse(@ModelAttribute Course course) {
		courseService.saveCourse(course);
		return "forward:/indexCourse";
	}

	/**
	 * Create a new Course entity
	 * 
	 */
	@RequestMapping("/newCourse")
	public ModelAndView newCourse() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("course", new Course());
		mav.addObject("newFlag", true);
		mav.setViewName("course/editCourse.jsp");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Show all Course entities
	 * 
	 */
	@RequestMapping("/indexCourse")
	public ModelAndView listCourses() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("courses", courseService.loadCourses());

		mav.setViewName("course/listCourses.jsp");

		return mav;
	}

	/**
	 * Edit an existing Course entity
	 * 
	 */
	@RequestMapping("/editCourse")
	public ModelAndView editCourse(@RequestParam String banjiIdKey, @RequestParam Integer weekDayKey, @RequestParam Integer seqNoKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("course", courseDAO.findCourseByPrimaryKey(banjiIdKey, weekDayKey, seqNoKey));
		mav.setViewName("course/editCourse.jsp");

		return mav;
	}
}